TABLEAVG Function

Syntax

Average_Of_Values as N = TABLEAVG(C tablename,C filter,C field)

Arguments

tablename

The full drive, path, name, and extension of the table. If you omit the drive, path, and extension, Alpha Anywhere searches the directory of the current table.

filter

A character filter expression that evaluates to a logical value. Selects records to average.

field

An expression based on numeric or exponent numeric field(s) in the Lookup_Table.

Description

Returns the average value for a field in records of a specified table.

Discussion

Searches the specified Lookup_Table for one or more records that satisfy the specified Filter, and returns the average of the values contained in the Lookup_Expression. The Filter must return a logical value, either True (.T.) or False (.F.). For example, to choose all of the records in a table, use the logical constant ".T." as a filter. To choose only the records where the STATE field is equal to TN, use the filter "STATE = 'TN'". Note that the entire filter is always in quotations, and the character value, TN, is in single quotes. NOTE: If no records satisfy the filter, Alpha Anywhere returns a zero value.

Assume that a department purchasing table (PURCHASE) contains the following records:

PURCHASE TABLE
>
MANAGER
COST, BILL_DATE, and PAID_UP
ANDRY

18500.00 01/01/2003 T

BROWN

49.95 02/01/2003 T

BROWN

12.99 04/01/2003 F

FARLEY

7995.00 03/01/2003 F

FARLEY

142.16 04/10/2003 T

FARLEY

423.00 05/10/2003 F

Note : for detailed explanations of the filter expressions used in these examples, see TABLECOUNT().

All Records

The following expression returns the average of the values in the Cost field for all records in the Purchase table.

? TABLEAVG("PURCHASE.DBF", ".T.", "COST")
= 4520.516667

Filter Using a Specific Character String

The following expression returns the average of the values in the Cost field for all records in the Purchase table where "Brown" is the manager:

? TABLEAVG("PURCHASE.DBF", "MANAGER = 'BROWN'", "COST")
= 31.47

Filter Using a Character Variable

The following expression returns the average of the values in the Cost field for all records in the Purchase table where the manager matches the character string previously assigned to the variable vManager.

? TABLEAVG("PURCHASE.DBF","Manager = '" + vManager + "' ", "COST")
= 2853.386667 'if vManager is "FARLEY"

Filter Using a Specific Number

The following expression returns the average of the values in the Cost field for all records in the Purchase table where the Cost is greater than 1000.00.

? TABLEAVG("PURCHASE.DBF", "Cost > 1000.00", "COST")
= 13247.50

Filter Using Numeric Variable

The following expression returns the average of the values in the Cost field for all records in the Purchase table where the Cost is greater than whatever amount has been previously assigned to the numeric variable vCost.

? TABLEAVG("PURCHASE.DBF", "Cost > " + vCost, "COST")
= 13247.50 ' if vCost = 1000.00

Filter Using a Specific Date Value

The following expression returns the average of the values in the Cost field for all records in the Purchase table where the Bill_Date is equal to or later than January 1, 2003.

? TABLEAVG("PURCHASE.DBF", "BILL_DATE >= {01/01/2003}", "COST")
= 4520.516667

Filter Using Date Variables

The following expression returns the average of the values in the Cost field for all records in the Purchase table where the Bill_Date is equal to or later than the date previously assigned to vBeginDate AND equal to or later than the date previously assigned to vEndDate.

? TABLEAVG("PURCHASE.DBF","BETWEEN(BILL_DATE,{"+DTOC(vBeginDate)+"},{"+DTOC(vEndDate)+"})","COST")
= 77.575000 ' if vBeginDate is 04/01/2003 and vEndDate is 04/30/2003

Filter Using Logical Variables

The following expression returns the average of the values in the Cost field for all records in the Purchase table where Paid_Up is true:

? TABLEAVG("PURCHASE.DBF", "PAID_UP", "COST")
= 6230.703333

While this expression averages the values in the Cost field for all records in the Purchase table where Paid_Up is not true:

? TABLEAVG("PURCHASE.DBF", ".NOT. PAID_UP", "COST")
= 2810.330000

Thanks to

Tom Cone

See Also